package com.demo2do.fms.admin.service.supplier.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.AppraisalDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalTemplateItemDao;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService;
import com.demo2do.fms.entity.appraisal.AppraisalTemplate;
import com.demo2do.fms.entity.appraisal.AppraisalTemplateItem;

/**
 *
 * @author Ark
 *
 */
@Service("appraisalTemplateItemService")
public class AppraisalTemplateItemServiceImpl extends DataServiceImpl<AppraisalTemplateItem> implements AppraisalTemplateItemService {

    @Dao
    private AppraisalTemplateItemDao appraisalTemplateItemDao;
    
    @Dao
    private AppraisalDao appraisalDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService#count(com.demo2do.fms.entity.appraisal.AppraisalTemplate)
     */
    public int count(AppraisalTemplate appraisalTemplate) {
        return appraisalTemplateItemDao.count(appraisalTemplate);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService#list(com.demo2do.fms.entity.appraisal.AppraisalTemplate, com.demo2do.core.entity.page.Page)
     */
    public List<AppraisalTemplateItem> list(AppraisalTemplate appraisalTemplate, Page page) {
        page.calculate();
        return appraisalTemplateItemDao.list(appraisalTemplate, page.getBeginIndex(), page.getEveryPage());
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService#create(com.demo2do.fms.entity.appraisal.AppraisalTemplateItem, com.demo2do.fms.entity.appraisal.AppraisalTemplate, com.demo2do.fms.admin.entity.security.Login)
     */
    public void create(AppraisalTemplateItem item, AppraisalTemplate template, Login login) {
        
        // 1.1 valid whether has appraisal
        int totalAppraisal = appraisalDao.count(template);
        if (totalAppraisal > 0) {
            throw BusinessException.newInstance("500", "此标准已生成考核，无法变更");
        }
        
        // 1.2 valid unique
        AppraisalTemplateItem existItem = appraisalTemplateItemDao.getByName(template, item.getName());
        if (existItem != null) {
            throw BusinessException.newInstance("500", "已存在同名考核模板");
        }
        
        // 2. save item
        genericDaoSupport.save(item.initialize(template, login.getId()));
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService#edit(com.demo2do.fms.entity.appraisal.AppraisalTemplateItem, com.demo2do.fms.entity.appraisal.AppraisalTemplateItem, com.demo2do.fms.entity.appraisal.AppraisalTemplate, com.demo2do.fms.admin.entity.security.Login)
     */
    public void edit(AppraisalTemplateItem oldItem, AppraisalTemplateItem changeItem, AppraisalTemplate template, Login login) {
        
        // 1. valid whether edit item
        int totalAppraisal = appraisalDao.count(template);
        if (totalAppraisal > 0) {
            throw BusinessException.newInstance("500", "此标准已生成考核，无法变更");
        }
        
        // 2. edit item
        genericDaoSupport.update(oldItem.edit(changeItem, login.getId()));
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateItemService#delete(com.demo2do.fms.entity.appraisal.AppraisalTemplateItem, java.lang.Long)
     */
    public void delete(AppraisalTemplateItem item, Long operator) {
        
        // 1. valid whether edit item
        int totalAppraisal = appraisalDao.count(item.getParent());
        if (totalAppraisal > 0) {
            throw BusinessException.newInstance("500", "此标准已生成考核，无法删除");
        }
        
        // 2. delete
        genericDaoSupport.update(item.delete(operator));
        
    }
    
}
